package cn.bdqn.t354.service.impl;

import cn.bdqn.t354.mapper.DictMapper;
import cn.bdqn.t354.pojo.Dict;
import cn.bdqn.t354.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DictServiceImpl implements DictService {
    @Resource
    private DictMapper dictMapper;

    @Override
    public List<Dict> getDictByDictType(String dictType){
        QueryWrapper<Dict> qw=new QueryWrapper<>();
        qw.eq("dict_type",dictType);
        return dictMapper.selectList(qw);
    }

    @Override
    public IPage<Dict> getDictsByPage(String dictType,String dictItem,
                                      Integer pageNo, Integer pageSize) {
        //组装分页条件
        Page<Dict> page=new Page<>(pageNo,pageSize);
        //组装过滤条件 动态sql
        QueryWrapper<Dict> qw=new QueryWrapper();
        qw.like(dictType!=null&&!"".equals(dictType),"dict_type",dictType);
        qw.like(dictItem!=null&&!"".equals(dictItem),"dict_item",dictItem);

        //调用分页方法
        IPage<Dict> dictPage=dictMapper.selectPage(page, qw);
        //返回分页对象
        return dictPage;
    }


    @Override
    public void addDict(Dict role) {
        dictMapper.insert(role);
    }

    @Override
    public Dict getDict(Long id) {
        return dictMapper.selectById(id);
    }

    @Override
    public void updateDict(Dict dict) {
        dictMapper.updateById(dict);
    }

    @Override
    public void deleteDict(Long id) {
        dictMapper.deleteById(id);
    }
}
